package com.spring.cloud.alibaba.service.provider.controller;

import com.spring.cloud.alibaba.commons.params.StockParam;
import com.spring.cloud.alibaba.commons.pojo.ResponseVo;
import com.spring.cloud.alibaba.commons.pojo.StockInfo;
import com.spring.cloud.alibaba.service.provider.service.IStockService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/stock")
@RefreshScope
public class StockController {

    @Resource
    private IStockService stockService;

    @PostMapping("/saveOne")
    public ResponseVo saveOne(@RequestBody StockInfo stockInfo) {
        return stockService.saveOne(stockInfo);
    }

    @PostMapping("/saveBatch")
    public ResponseVo saveBatch(@RequestBody List<StockInfo> stockInfoEntityList) {
        return stockService.saveBatch(stockInfoEntityList);
    }

    @GetMapping(value = "/findStockByCode")
    public ResponseVo findStockByGoodCode(@RequestParam("goodCode") String goodCode){
        if(StringUtils.isEmpty(goodCode)) {
            throw new IllegalArgumentException("parameter goodCode cannot be null");
        }
        return stockService.findStockByGoodCode(goodCode);
    }

    @PostMapping("/updateStockCountById")
    public ResponseVo updateStockById(@RequestBody StockInfo stockInfo){
        if(stockInfo.getId()==null || stockInfo.getId()<=0){
            throw new IllegalArgumentException("parameter id cannot small than 0");
        }
        if(stockInfo.getCount() < 0) {
            throw new IllegalArgumentException("parameter count cannot small than 0");
        }
        return stockService.updateStockById(stockInfo);
    }

    @GetMapping("/list/condition")
    public ResponseVo listByCondition(@RequestBody StockParam stockParam) {
        return stockService.queryListByCondition(stockParam);
    }

    @GetMapping("/page/condition/{pageNo}/{pageSize}")
    public ResponseVo selectPageByCondition(@PathVariable("pageNo") int pageNo, @PathVariable("pageSize") int pageSize,
                                            @RequestBody StockParam stockParam) {
        return stockService.queryPageDataByCondition(pageNo, pageSize, stockParam);
    }
}
